from command import *
from typing import List


class RemoteControl:
    def __init__(self):
        self._on_commands: List[Command] = []
        self._off_commands: List[Command] = []
        self._command_history: List[Command] = []

        for i in range(7):
            self._on_commands.append(NoCommand())
            self._off_commands.append(NoCommand())

    def set_command(self, slot: int, on_command: Command, off_command: Command):
        self._on_commands[slot] = on_command
        self._off_commands[slot] = off_command

    def on_button_was_pushed(self, slot: int):
        self._on_commands[slot].execute()
        self._command_history.append(self._on_commands[slot])

    def off_button_was_pushed(self, slot: int):
        self._off_commands[slot].execute()
        self._command_history.append(self._off_commands[slot])

    def undo_button_was_pushed(self):
        if self._command_history:
            self._command_history.pop().undo()

    def __str__(self):
        result = ["\n----- Remote Control -----\n"]
        for i in range(len(self._on_commands)):
            result.append(f"[slot {i}] {type(self._on_commands[i])}    {type(self._off_commands[i])}\n")
        result.append(f"[undo] {self._command_history}")
        return "".join(result)


if __name__ == '__main__':
    from light import *
    from hot_tub import *
    from stereo import *
    from tv import *

    remote_control = RemoteControl()

    light = Light("Living Room")
    tv = TV("Living Room")
    stereo = Stereo("Living Room")
    hot_tub = HotTub()

    light_on = LightOnCommand(light)
    stereo_on = StereoOnCommand(stereo)
    tv_on = TVOnCommand(tv)
    hot_tub_on = HotTubOnCommand(hot_tub)

    light_off = LightOffCommand(light)
    stereo_off = StereoOffCommand(stereo)
    tv_off = TVOffCommand(tv)
    hot_tub_off = HotTubOffCommand(hot_tub)

    party_on = MacroCommand([light_on, stereo_on, tv_on, hot_tub_on])
    party_off = MacroCommand([light_off, stereo_off, tv_off, hot_tub_off])

    remote_control.set_command(0, party_on, party_off)

    print(remote_control)
    print("--- Pushing Macro On ---")
    remote_control.on_button_was_pushed(0)
    print("--- Pushing Macro Off ---")
    remote_control.off_button_was_pushed(0)